export default {
	namespaced: true,
	state() {
		return {
			token: '',
			//用户基本信息
			userInfo: {}
		}
	},
	mutations: {
		// 设置token
		setToken(state, token) {
			state.token = token
		},
		// 移出token
		removeToken(state) {
			state.token = ''
		},
		// 设置用户信息
		setUserInfo(state, info) {
			state.userInfo = info

			console.log('%c [  ]-24', 'font-size:13px; background:pink; color:#bf2c9f;', '更新信息成功')
		},
		// 更新用户信息
		// 更新头像
		updateUserInfo(state, info) {
			state.userInfo.avatar = info.avatar
			state.userInfo.email = info.email
			state.userInfo.phone = info.phone
		}
	},

	getters: {
		// 获取用户信息
		getUser(state) {
			return state.userInfo
		}
	},

	actions: {
		async getUserInfo(context, userId) {
			try {
				const { data } = await uni.$http.get(`getInfo/${userId}`)
				console.log('getUserInfo', data)
				context.commit('setUserInfo', data.data)
			} catch (error) {
				console.log('%c [ store/user.js/getUserInfo ]-45', 'font-size:13px; background:pink; color:#bf2c9f;', error)
			}
		},
		//更新用户信息
		async updateUserAsync(context, updateInfo) {
			try {
				const res = await uni.$http.post('update', updateInfo)
				if (res && res.code == 200) {
					context.getUserInfo(context.state.token)
				}
			} catch (error) {
				console.log('%c [ updateUserAsync ]-55', 'font-size:13px; background:pink; color:#bf2c9f;', error)
			}
		}
	}
}
